
LyLock_Armor = { }
table.insert(LyLock_Plugins, LyLock_Armor)
LyLock_Armor.Frame = "LyLockArmorFrame"
LyLock_Armor.Button = "LyLockArmorButton"

------------------------------------------------------
-- Armor Settings ------------------------------
------------------------------------------------------



LyLock_Armor.OnSettings = function(action)
    if action=="default" then

        LyLock_Settings.Armor_Button = ReturnSetting(LyLock_Settings.Armor_Button, true)
        LyLock_Settings.Armor_Tooltip = ReturnSetting(LyLock_Settings.Armor_Tooltip, true)
        LyLock_Settings.Armor_FelArmor = ReturnSetting(LyLock_Settings.Armor_FelArmor, true)
        LyLock_Settings.Armor_MsgScreen = ReturnSetting(LyLock_Settings.Armor_MsgScreen, true)
        LyLock_Settings.Armor_FlashMsg = ReturnSetting(LyLock_Settings.Armor_FlashMsg, true)
        LyLock_Settings.Armor_ReadyUse = ReturnSetting(LyLock_Settings.Armor_ReadyUse, true)
        LyLock_Settings.Armor_Unbuffed = ReturnSetting(LyLock_Settings.Armor_Unbuffed, true)
        LyLock_Settings.Armor_Interval = ReturnSetting(LyLock_Settings.Armor_Interval, 10)
        
    elseif action=="save" then
    
        LyLock_Settings.Armor_Button = iif(Armor_Button:GetChecked(), true, false);
        LyLock_Settings.Armor_Tooltip = iif(Armor_Tooltip:GetChecked(), true, false);
        LyLock_Settings.Armor_FelArmor = iif(Armor_FelArmor:GetChecked(), true, false);
        LyLock_Settings.Armor_MsgScreen = iif(Armor_MsgScreen:GetChecked(), true, false);
        LyLock_Settings.Armor_FlashMsg = iif(Armor_FlashMsg:GetChecked(), true, false);
        LyLock_Settings.Armor_ReadyUse = iif(Armor_ReadyUse:GetChecked(), true, false);
        LyLock_Settings.Armor_Unbuffed = iif(Armor_Unbuffed:GetChecked(), true, false);
        
    elseif action=="load" then
    
        Armor_Button:SetChecked(LyLock_Settings.Armor_Button);
        Armor_Tooltip:SetChecked(LyLock_Settings.Armor_Tooltip);
        Armor_FelArmor:SetChecked(LyLock_Settings.Armor_FelArmor);
        Armor_MsgScreen:SetChecked(LyLock_Settings.Armor_MsgScreen);
        Armor_FlashMsg:SetChecked(LyLock_Settings.Armor_FlashMsg);
        Armor_ReadyUse:SetChecked(LyLock_Settings.Armor_ReadyUse);
        Armor_Unbuffed:SetChecked(LyLock_Settings.Armor_Unbuffed);
        
    end

    LyLock_Armor.ButtonSetupAction()

end


--------------------------------------------------------------------------------------
-- Armor Functions -------------------------------------------------------------
--------------------------------------------------------------------------------------

LyLock_Armor.Message=0
LyLock_Armor.Flasher=0
LyLock_Armor.ToolInfo = ""


LyLock_Armor.ButtonSetupAction = function()

    if LyLock_Settings.Armor_FelArmor then
    

        local rank = GetSpellRank(WORD_FELARMOR)
        if rank~=nil then
            LyLockArmorButton:SetAttribute("type", "macro");
            LyLockArmorButton:SetAttribute("macrotext", "/cast "..WORD_FELARMOR.."("..rank..")");
        end
        local rank = GetSpellRank(WORD_DEMONARMOR)
        if rank~=nil then
            LyLockArmorButton:SetAttribute("shift-type*", "spell");
            LyLockArmorButton:SetAttribute("spell", WORD_DEMONARMOR.."("..rank..")");
        else
            rank = GetSpellRank(WORD_DEMONSKIN)
            if rank~=nil then
                LyLockArmorButton:SetAttribute("shift-type*", "spell");
                LyLockArmorButton:SetAttribute("spell", WORD_DEMONSKIN.."("..rank..")");
            end            
        end

        LyLock_Armor.ToolInfo = COLOR_BRIGHTGREEN.."Click for Fel Armor\nShift-Click for Demon Armor"
        
        LyLockArmorButton:SetNormalTexture("Interface\\AddOns\\LyLock\\GUI\\FelArmor_Normal");
        LyLockArmorButton:SetHighlightTexture("Interface\\AddOns\\LyLock\\GUI\\FelArmor_Active");                        

        
    else
        local rank = GetSpellRank(WORD_DEMONARMOR)
        if rank~=nil then
            LyLockArmorButton:SetAttribute("type", "spell");
            LyLockArmorButton:SetAttribute("spell", WORD_DEMONARMOR.."("..rank..")");
        else
            rank = GetSpellRank(WORD_DEMONSKIN)
            if rank~=nil then
                LyLockArmorButton:SetAttribute("type", "spell");
                LyLockArmorButton:SetAttribute("spell", WORD_DEMONSKIN.."("..rank..")");
            end
        end
        local rank = GetSpellRank(WORD_FELARMOR)
        if rank~=nil then
            LyLockArmorButton:SetAttribute("shift-type*", "macro");
            LyLockArmorButton:SetAttribute("macrotext", "/cast "..WORD_FELARMOR.."("..rank..")")
        end
        LyLock_Armor.ToolInfo = COLOR_BRIGHTGREEN.."Click for Demon Armor\nShift-Click for Fel Armor"
        
        LyLockArmorButton:SetNormalTexture("Interface\\AddOns\\LyLock\\GUI\\DemonArmor_Normal");
        LyLockArmorButton:SetHighlightTexture("Interface\\AddOns\\LyLock\\GUI\\DemonArmor_Active");   
    end
end

LyLock_Armor.ButtonTooltipText = function()
    if LyLock_Settings.Armor_Tooltip then
        return LyLock_Armor.ToolInfo
    end
end


LyLock_Armor.OnSecond = function()
	if UnitIsDeadOrGhost("player")==nil then
		if not IsUnitArmor() then
            
            if LyLock_Armor.Message==0 then
                LyLock_Armor.Message = GetTime()
                LyLock_Armor.Flasher = GetTime()
                if LyLock_Settings.Armor_ReadyUse then
                    if LyLock_Settings.Armor_MsgScreen then
                        ScreenMessage("Armor is now ready to be used");
                    end
                    EchoMessage("LyLock", "Armor is now ready to be used", "ReadyUse")
                    LyLockArmorReadyText:SetText(LYLOCK_FLAGCAST)
                end
            else
                if (GetTime()-LyLock_Armor.Message)>=(LyLock_Settings.Armor_Interval*60) then
                    LyLock_Armor.Message = GetTime()
                    LyLock_Armor.Flasher = GetTime()
                    if LyLock_Settings.Armor_Unbuffed then
                        if LyLock_Settings.Armor_MsgScreen then
                            ScreenMessage("Armor is good; I'd get some");
                        end
                        EchoMessage("LyLock", "Armor is good; I'd get some", "UnBuffed")
                        LyLockArmorReadyText:SetText(LYLOCK_FLAGCAST)
                    end
                else
                    if (GetTime()-LyLock_Armor.Flasher)>=10 then
                        LyLock_Armor.Flasher = GetTime()
                        if (LyLock_Settings.Armor_FlashMsg and LyLock_Settings.Armor_MsgScreen) then
                            ScreenMessage("Armor is good; I'd get some");
                        end
                    end
                end
            end

		else
            if (LyLock_Armor.Message~=0) then
                LyLock_Armor.Message=0
                LyLockArmorReadyText:SetText(LYLOCK_FLAGNONE)
            end
		end
	end
end


function IsUnitArmor()
	return IsUnitBuff("player", TEXTURE_DEMONARMOR) or IsUnitBuff("player", TEXTURE_FELARMOR)
end